home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 567 < prev    next >
Internet Message Format  |  1996-08-06  |  2KB

  1. Path: mail2news.demon.co.uk!pires.demon.co.uk
  2. From: Martin Bonner <mbonner@pires.co.uk>
  3. Newsgroups: comp.std.c
  4. Subject: Re: alignment requirements for all structures?
  5. Date: Thu, 14 Mar 1996 12:41:28 GMT
  6. Organization: Pi Technology Ltd
  7. Distribution: world
  8. Message-ID: <814065992wnr@pires.co.uk>
  9. References: <1996Mar7.151216.1793@ittpub>
  10. Reply-To: mbonner@pires.co.uk
  11. X-NNTP-Posting-Host: pires.demon.co.uk
  12. X-Broken-Date: Thursday, Mar 14, 1996 12.41.28 GMT
  13. X-Newsreader: Newswin Alpha 0.6
  14. X-Mail2News-Path: relay-4.mail.demon.net!post.demon.co.uk!pires.demon.co.uk
  15.  
  16. In article: <1996Mar7.151216.1793@ittpub>  wil@ittpub.nl (Wil Evers) writes:
  17. > (1). Is there a rule in the C standard stating that the address of *every*  
  18. > structure - irrespective of its contents - should satisfy some alignment  
  19. > requirement? For example, is the following guaranteed to work?
  20. >     struct a { char data[sizeof(double)]; } ana;
  21. >     struct b { double data; };
  22. >     int main()
  23. >     {
  24. >         ((struct b *) &ana)->data = 42.42;
  25. >         return 0;
  26. >     }
  27. I am fairly sure not.
  28.  
  29. > (2). If the answer to (1) is no, then is a conforming compiler allowed to  
  30. > assign some specific alignment requirement to a data type, just because it  
  31. > is defined as a structure, and again irrespective of the contents of the  
  32. > structure? For example, if I write:
  33. >     
  34. >     #include <stdio.h>
  35. >     struct c { char data; } carray[10];
  36. >     int main()
  37. >     {
  38. >         printf("%u\n", sizeof carray);
  39. >         return 0;
  40. >     }
  41. > would it be legal for this program to print something other than 10?
  42. Almost certainly YES
  43. > (3). If the answer to (2) is yes, why would a compiler want to do this?
  44. For example:
  45. Because the architecture is word addressed (not byte addressed), so 
  46. indexing the array is painful unless the structures are padded up to
  47. machine words.  (I can think of other reasons too).
  48.  
  49. In general, ISO C does not specify ANYTHING about additional padding that a 
  50. compiler may introduce
  51.  
  52. Martin Bonner
  53. Pi Technology, Milton Hall, Cambridge, ENGLAND
  54. +44 1223 441256
  55. mbonner@pires.co.uk
  56.  
  57.